package common.web.action;

import java.text.MessageFormat;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import common.log.LoggerProxy;
import common.log.util.LogModule;
import common.log.util.LogType;
import common.log.vo.WebLog;
import common.rbac.po.SystemConfig;
import common.util.JSONUtils;
import common.web.vo.Result;

@Controller
@RequestMapping("/system/config")
public class SystemConfigAction extends CommonAction {

	private static final LoggerProxy logger = LoggerProxy.getLogger(SystemConfigAction.class);

	@RequestMapping("/view/{id}")
	@ResponseBody
	public Result view(@PathVariable String id) {
		SystemConfig config = this.baseService.get(SystemConfig.class, id);
		if (null == config) {
			return Result.fail("系统配置不存在");
		}
		Result result = Result.createResult(config);
		return result;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Result list(@RequestBody JSONObject json) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SystemConfig.class);
		super.handleFilter(SystemConfig.class, json, criteria);
		super.handleSort(json, criteria);
		return Result.createPageResult(baseService.findByCriteriaForPage(criteria, super.getPageNumber(json),
				super.getPageSize(json)));
	}


	@RequestMapping("/{editType}")
	@ResponseBody
	public Result save(@PathVariable String editType, @RequestBody JSONObject json) {
		SystemConfig config = JSONUtils.toJavaObject(json.getJSONObject("entity"), SystemConfig.class);
		if (null != config) {
			WebLog log = new WebLog(LogModule.SYSTEMCONFIG, LogType.ADD, "操作失败");
			if ("update".equals(editType)) {
				log.setType(LogType.UPDATE);
			}
			try {
				// 删除关联部分
				if ("update".equals(editType)) {
					super.setCommonUpdateInfo(config);
					baseService.update(config);
					log.setSuccess(MessageFormat.format("修改系统配置[{0},{1}]成功!", config.getType(),config.getLabel()));
				} else if ("add".equals(editType)) {
					super.setCommonSaveInfo(config);
					baseService.save(config);
					log.setSuccess(MessageFormat.format("新增系统配置[{0},{1}]成功!", config.getType(),config.getLabel()));
				}
			} catch (Exception e) {
				log.setFail(MessageFormat.format("操作系统配置[{0},{1}]失败!", config.getType(),config.getLabel()));
				logger.error("", e);

				return Result.fail("数据错误，请刷新后重试");
			} finally {
				logService.saveLog(log);
			}
		} else {
			return Result.fail("数据错误，请刷新后重试");
		}
		return Result.SUCCESS;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Result delete(@PathVariable String id) {
		SystemConfig load = baseService.load(SystemConfig.class, id);
		if (null != load) {
			WebLog log = new WebLog(LogModule.SYSTEMCONFIG, LogType.DELETE, "操作失败");
			try {
				super.baseService.delete(load);
				// 日志
				log.setSuccess(MessageFormat.format("删除系统配置[{0},{1}]成功!", load.getType(),load.getLabel()));
			} catch (Exception e) {
				log.setFail(MessageFormat.format("删除系统配置[{0},{1}]失败!", load.getType(),load.getLabel()));
				logger.error("", e);

				return Result.fail("删除失败");
			} finally {
				logService.saveLog(log);
			}
		} else {
			return Result.fail("系统配置不存在");
		}
		return Result.SUCCESS;
	}
}
